<template>
  <div class="setting-item" :class="{ 'child-item': depth > 0 }">
    <div class="setting-row">
      <!-- 左侧分类名称 -->
      <span class="setting-label" >{{ item.label }}</span>
      
      <!-- 右侧选项列表 -->
      <div class="setting-options"      >
        <!-- 添加"全部"选项 -->
        <span 
          class="option" 
          v-if="item.children.length && !item.notAll"
          :class="{ 'active': !selectedPath[depth] }"
          @click="handleSelectAll"
        >
          全部
        </span>
        <!-- 其他选项 -->
        <span 
          v-for="child in item.children" 
          :key="child.id"
          @click="handleSelect(child)"
          class="option" 
          :class="{ 'active': isActive(child.id) }"
        >
          {{ child.name || child.label }}
        </span>
      </div>
    </div>
    
    <!-- 递归渲染子节点 - 修改条件判断，确保子节点正确显示且不超过最大层级 -->
    <div 
      v-if="selectedNode && 
            !isLocalDataNode(selectedNode) && 
            !isMaxLevelReached(selectedNode) && 
            selectedNode.children" 
      class="child-options"
    >
      <TreeOption 
        :item="selectedNode" 
        :depth="depth + 1"
        :selected-path="selectedPath"
        @select="handleChildSelect"
      />
    </div>
  </div>
</template>
<script setup>
import { ref, watch, onMounted } from 'vue';

const props = defineProps({
  item: {
    type: Object,
    required: true
  },
  depth: {
    type: Number,
    default: 0
  },
  selectedPath: {
    type: Array,
    default: () => []
  }
});

const emit = defineEmits(['select']);

// 当前层级选中的节点
const selectedNode = ref(null);

// 检查节点是否为本地数据节点
const isLocalDataNode = (node) => {
  return node && (node.isLocalData === true || 
                  (node.id && ((node.id+'').startsWith('time-') || 
                  (node.id+'').startsWith('count-'))));
};

const isMaxLevelReached = (node) => {
  // 检查是否有isMaxCount属性并且当前深度已经达到或超过该值
  return node && node.isMaxCount !== undefined && props.depth >= node.isMaxCount;
};

// 计算当前路径下是否选中
const isActive = (id) => {
  return props.selectedPath[props.depth] === id;
};

// 处理选择"全部"
const handleSelectAll = () => {
  selectedNode.value = null;
  
  // 创建新的选择路径
  const newPath = [...props.selectedPath];
  // 清除当前层级及之后的选择
  newPath.splice(props.depth);
  
  // 向上传递选择事件
  emit('select', {
    node: null,
    path: newPath,
    depth: props.depth
  });
};

const handleSelect = (node) => {
  // 设置选中节点
  selectedNode.value = node;
  
  // 创建新的选择路径
  const newPath = [...props.selectedPath];
  newPath[props.depth] = node.id;
  
  // 清除此层级之后的所有选择
  newPath.splice(props.depth + 1);
  
  // 检查是否为本地数据节点或是否达到最大层级
  const isLocal = isLocalDataNode(node);
  const isMaxLevel = isMaxLevelReached(node);
  
  // 向上传递选择事件，通知父组件可能需要加载子节点数据
  emit('select', {
    node,
    path: newPath,
    depth: props.depth,
    needChildren: !isLocal && !isMaxLevel && node.children && node.children.length === 0, // 标记是否需要加载子节点
    isLocalData: isLocal, // 标记是否为本地数据
    isMaxLevel: isMaxLevel, // 标记是否达到最大层级
    isItTimeLimited: node.isItTimeLimited // 传递是否需要时间限制的标记
  });
};

// 处理子节点选择
const handleChildSelect = (data) => {
  emit('select', data);
};

// 监听选择路径变化，更新选中节点
watch(() => props.selectedPath, (newPath) => {
  if (newPath && newPath[props.depth]) {
    const nodeId = newPath[props.depth];
    const foundNode = props.item.children.find(child => child.id === nodeId);
    if (foundNode) {
      selectedNode.value = foundNode;
    }
  } else {
    selectedNode.value = null;
  }
}, { immediate: true });

// 组件挂载时，根据路径初始化选中节点
onMounted(() => {
  if (props.selectedPath && props.selectedPath[props.depth]) {
    const nodeId = props.selectedPath[props.depth];
    const foundNode = props.item.children.find(child => child.id === nodeId);
    if (foundNode) {
      selectedNode.value = foundNode;
    }
  }
});
</script>

<style scoped lang="scss">
.setting-item {
  margin-bottom: 15px;
  
  &.child-item {
    // margin-left: 80px;
    // margin-top: 15px;
  }
}

.setting-row {
  display: flex;
  // margin-bottom: 10px;
  // padding-bottom: 10px;
  border-bottom: 1px solid #eee;
  width: 100%;
}

.setting-label {
  flex: 0 0 60px;
  color: #333;
  font-size: 14px;
  font-weight: 500;
  line-height: 32px;
}

.setting-options {
  flex: 1;
  display: flex;
  flex-wrap: wrap;
  // gap: 10px;
  align-items: center;
}

.option {
  padding: 6px 0px;
  border-radius: 4px;
  font-size: 14px;
  color: #666;
  cursor: pointer;
  margin:0px 5px;
  transition: all 0.3s;
  
  &:hover {
    // background-color: #f0f0f0;
  }
  
  &.active {
    color: #2B5B7B;
    font-weight: 500;
    // background-color: rgba(43, 91, 123, 0.1);
  }
}

.child-options {
  // margin-top: 10px;
  width:100%
}
</style>